<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/12/17
 * Time: 14:10
 */


global $_key_log_action_titles;

$_key_log_action_titles = array(
    'create' => '添加',
    'delete' => '删除',
    'update' => '更新',
    'disable' => '禁用',
    'enable' => '启用',
    'refund' => '退款',
    'confirm' => '确认',
    'reset' => '重置',
    'reject' => '驳回',
    'simulate_login' => '模拟登录'
);

//添加关键记录
function add_key_log($memberId, $item, $itemId = 0, $desc = '')
{
    //代添加数据
    $data = array(
        'administrator_id' => $_SESSION['administrator']['id'],
        'ip' => query_real_ip(),
        'create_time' => now_datetime(),
        'member_id' => $memberId,
        'item' => $item,
        'item_id' => $itemId,
        'description' => $desc
    );

    //插入数据
    M("KeyLog")->add($data);
}

//添加用户相关关键记录
function add_member_key_log($action, $memberId)
{
    global $_key_log_action_titles;

    //会员昵称
    $nickname = M("Member")->where(array("id"=>$memberId))->getField("username");

    //描述信息
    $desc = $_key_log_action_titles[$action] . ' 会员【' . $nickname . '】编号：' . $memberId;

    //添加关键记录
    add_key_log($memberId, 'member_' . strtolower($action), $memberId, $desc);
}

//添加取现记录
function add_member_cash_flow($memberId, $item, $itemId, $amount, $balance, $stock, $recharge,$referral_bonus, $memo = '')
{
    M("CashFlow")
    ->add(
        array(
            "member_id" => $memberId,
            "create_time" => now(),
            "date" => date("Y-m-d"),
            "item" => $item,
            "item_id" => $itemId,
            "amount" => $amount,
            "balance" => $balance,
            "recharge" => $recharge,
            "stock" => $stock,
            "referral_bonus" => $referral_bonus,
            "memo" => $memo
        )
    );
}

// 添加充值核心日志
function add_recharge_key_log($action, $member, $recharge)
{
    global $_key_log_action_titles;

    add_key_log($member['id'], 'recharge_' . strtolower($action), $recharge['id'],
        $_key_log_action_titles[$action] . ' ' . '充值  ￥' . $recharge['amount'] .
        '，单号：' . $recharge['sn'] . '，编号' . $recharge['id'] .
        ' 会员【 ' . $member['username'] . '】编号：' . $member['id']);
}

//添加提现核心日志
function add_withdrawal_key_log($action, $member, $withdrawal)
{
    global $_key_log_action_titles;

    add_key_log($member['id'], 'withdrawal_' . strtolower($action), $withdrawal['id'],
        $_key_log_action_titles[$action] . ' ' . '提现  ￥' . $withdrawal['amount'] .
        '，单号：' . $withdrawal['sn'] . '，编号' . $withdrawal['id'] .
        ' 会员【 ' . $member['username'] . '】编号：' . $member['id']);
}

//向用户发送消息
function send_user_message($memberId, $title, $content)
{
    if($memberId)
    {
        $messageId = M("Message")
            ->add(array(
                "administrator_id" => $_SESSION['administrator']['id'],
                'title' => $title,
                'content' => $content,
                'mass' => 0,
                'member_count' => 1,
                'members' => $memberId,
                'status' => 'send',
                'create_time' => now(),
                'update_time' => now()
            ));

        if($messageId)
        {
            M("MemberMessage")
                ->add(array(
                    "member_id" => $memberId,
                    "message_id" => $messageId,
                    "status" => "unread",
                    "create_time" => now(),
                ));
        }
    }
    return 0;
}